package com.jin.spring_boot_learn.concurrent.locks.reentranceLock;

import java.util.concurrent.*;
import java.util.concurrent.locks.ReentrantLock;
import java.util.stream.IntStream;

/**
 * 公平的锁上，线程按照他们发出请求的顺序获取锁
 */
public class ReentrantLockTest03 {
    private static final ReentrantLock lock = new ReentrantLock(true);


    static Runnable thread = () -> {
        lock.lock();
        try {
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            System.out.println("当前线程的名称:"+ Thread.currentThread().getName());
        }finally {
            lock.unlock();

        }
    };

    public static void main(String[] args) {
        ExecutorService executorService = Executors.newCachedThreadPool();
        IntStream.range(1,10).forEach((i)->executorService.submit(thread));
        executorService.shutdown();


    }
}
